package com.ms.education.statistics.config;

import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>Project:我的代码-ElasticjobConfig
 * <p>Powered by ms on 2023-02-11 16:31:09
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@Configuration
public class ElasticJobConfig {

    @Value("${elasticjob.serverlists}")
    private String serverLists;

    @Value("${elasticjob.namesapce}")
    private String namespace;

    /*
       注册中心的配置，可以设置连接注册中心的参数
     */
    @Bean
    public ZookeeperConfiguration zookeeperConfiguration() {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverLists, namespace);
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        return zookeeperConfiguration;
    }

    /*
       添加注册中心
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter registryCenter(ZookeeperConfiguration zookeeperConfiguration) {
        return new ZookeeperRegistryCenter(zookeeperConfiguration);
    }

    @Bean
    public ElasticJobListener elasticJobListener() {
        return new ElasticJobListener() {
            @Override
            public void beforeJobExecuted(ShardingContexts shardingContexts) {

            }

            @Override
            public void afterJobExecuted(ShardingContexts shardingContexts) {

            }
        };
    }
}
